package com.yjc.drygoods.member.web;

import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yjc.common.to.CouponTo;
import com.yjc.common.to.SkuInfoTo;
import com.yjc.common.utils.PageUtils;
import com.yjc.common.utils.R;
import com.yjc.common.vo.MemberRespVo;
import com.yjc.drygoods.member.entity.MemberReceiveAddressEntity;
import com.yjc.drygoods.member.feign.CouponFeignService;
import com.yjc.drygoods.member.feign.OrderFeignService;
import com.yjc.drygoods.member.feign.ProductFeignService;
import com.yjc.drygoods.member.interceptor.LoginUserInterceptor;
import com.yjc.drygoods.member.service.MemberCollectSkuService;
import com.yjc.drygoods.member.service.MemberReceiveAddressService;
import com.yjc.drygoods.member.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

/**
 * @author yjc
 * @create 2022-09-29 11:22
 */
@Slf4j
@Controller
public class MemberWebController {

    @Autowired
    OrderFeignService orderFeignService;

    @Autowired
    MemberService memberService;

    @Autowired
    MemberReceiveAddressService memberReceiveAddressService;

    @Autowired
    MemberCollectSkuService memberCollectSkuService;

    @Autowired
    ProductFeignService productFeignService;

    @Autowired
    CouponFeignService couponFeignService;

    /**
     * 取消关注
     */
    @GetMapping("/unfollow/{skuId}")
    public String unfollow(@PathVariable("skuId") Long skuId) {
        memberCollectSkuService.unfollow(skuId);
        Map<String, Object> map = new HashMap<>();
        map.put("skuId", skuId);
        map.put("flag", false);
        productFeignService.updateAttentionCount(map);
        return "redirect:http://member.drygoods.top/followProduct.html";
    }


    @GetMapping("/orderList.html")
    public String orderListPage(@RequestParam Map<String, Object> params, Model model) {
        MemberRespVo memberRespVo = LoginUserInterceptor.loginUser.get();
        log.info("params:{}", params);
        R r = orderFeignService.listWithOrderItems(params);
        if (r.getCode() == 0) {
//            System.out.println(JSON.toJSONString(r));
            model.addAttribute("orderWithItems", r);
        }
        //查询当前用户可能喜欢的商品信息
        R res = productFeignService.getUserLikeSkuInfo(memberRespVo.getUsername());
        if (res.getCode() == 0) {
            List<SkuInfoTo> data = res.getData(new TypeReference<List<SkuInfoTo>>() {
            });
            model.addAttribute("likeSkuInfos", data);
        }
        return "orderList";
    }

    /**
     * 会员基本信息页面
     *
     * @param model
     * @return
     */
    @GetMapping("/members.html")
    public String memberPage(Model model) {
        MemberRespVo memberInfo = LoginUserInterceptor.loginUser.get();
//        log.info("会员信息：{}", memberInfo);
        model.addAttribute("memberInfo", memberInfo);
        return "members";
    }

    /**
     * 会员头像信息页面
     *
     * @param model
     * @return
     */
    @GetMapping("/membersHeader.html")
    public String memberHeaderPage(Model model) {
        MemberRespVo memberInfo = LoginUserInterceptor.loginUser.get();
        model.addAttribute("memberInfo", memberInfo);
        return "membersHeader";
    }

    /**
     * 会员更多信息页面
     *
     * @param model
     * @return
     */
    @GetMapping("/membersMoreInfo.html")
    public String memberMoreInfoPage(Model model) {
        MemberRespVo memberInfo = LoginUserInterceptor.loginUser.get();
        model.addAttribute("memberInfo", memberInfo);
        return "membersMoreInfo";
    }

    /**
     * 会员账号绑定页面
     *
     * @param model
     * @return
     */
    @GetMapping("/membersBinding.html")
    public String membersBindingPage(Model model) {
        MemberRespVo memberInfo = LoginUserInterceptor.loginUser.get();
        model.addAttribute("memberInfo", memberInfo);
        return "membersBinding";
    }

    /**
     * 会员收货地址页面
     *
     * @param model
     * @return
     */
    @GetMapping("/membersAddress.html")
    public String membersAddressPage(Model model) {
        MemberRespVo memberInfo = LoginUserInterceptor.loginUser.get();
        List<MemberReceiveAddressEntity> memberAddresses
                = memberReceiveAddressService.list(new QueryWrapper<MemberReceiveAddressEntity>().eq("member_id", memberInfo.getId()));
//        memberAddresses.forEach(System.out::println);
        model.addAttribute("memberAddresses", memberAddresses);
        return "membersAddress";
    }

    /**
     * 会员关注商品界面
     *
     * @param model
     * @param params
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @GetMapping("/followProduct.html")
    public String followProductPage(Model model, @RequestParam Map<String, Object> params) throws ExecutionException, InterruptedException {
        MemberRespVo memberRespVo = LoginUserInterceptor.loginUser.get();
        PageUtils memberCollectSkus = memberCollectSkuService.getTotalFollowProductInfo(params);
        model.addAttribute("memberCollectSkus", memberCollectSkus);
        //查询当前用户可能喜欢的商品信息
        R res = productFeignService.getUserLikeSkuInfo(memberRespVo.getUsername());
        if (res.getCode() == 0) {
            List<SkuInfoTo> data = res.getData(new TypeReference<List<SkuInfoTo>>() {
            });
            model.addAttribute("likeSkuInfos", data);
        }
        return "followProduct";
    }

    @GetMapping("/couponList.html")
    public String couponListPage(Model model) {
        MemberRespVo memberRespVo = LoginUserInterceptor.loginUser.get();
        R r = couponFeignService.getCouponByMemberId(memberRespVo.getId());
        if (r.getCode() == 0) {
            List<CouponTo> data = r.getData(new TypeReference<List<CouponTo>>() {
            });
            model.addAttribute("memberCouponInfos", data);
        }
        return "couponList";
    }



}
